﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using Gao.Poker.App.Game;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using Gao.Poker.ViewModel;
using Gao.Warrior.WPF.Core.Command;

namespace Gao.Poker.App.ViewModel
{
    public class GameViewModel : BaseViewModel
    {
        /// <summary>
        /// 模板
        /// </summary>
        private readonly ICommand _TemplateCommand;
        /// <summary>
        /// 新增名次
        /// </summary>
        private readonly ICommand _AddBounsCommand;
        /// <summary>
        /// 新增比赛级别
        /// </summary>
        private readonly ICommand _AddLevelCommand;
    
        private Competition _Competition;
        private List<Competition> _Competitions;
        private List<Competition> _AllCompetitions;
        private List<MemberSeat> _members;
        private List<Tables> _tableses;
        private List<Ranking> _rankings;
        private int _pageIndex;
        private int _allCompetitionTotal;

        public String CompetitionName { get; set; }

        public GameViewModel()
        {
            CompetitionName = String.Empty;
            if (_rankings == null) _rankings = new List<Ranking>();
            _TemplateCommand = new RelayCommand(OnShowTemplate);
            _AddBounsCommand = new RelayCommand(OnAddBouns);
            _AddLevelCommand = new RelayCommand(OnAddLevel);
        }
        /// <summary>
        /// 选择模板
        /// </summary>
        public ICommand TemplateCommand { get { return _TemplateCommand; } }
        /// <summary>
        /// 新增名次
        /// </summary>
        public ICommand AddBounsCommand { get { return _AddBounsCommand; } }
        /// <summary>
        /// 新增比赛级别
        /// </summary>
        public ICommand AddLevelCommand { get { return _AddLevelCommand; } }

        /// <summary>
        /// 选择模板
        /// </summary>
        /// <param name="obj"></param>
        private void OnShowTemplate(object obj)
        {
            SelectTemplateWin win = new SelectTemplateWin(OnSelectedTemplate);
            win.Owner = Application.Current.MainWindow;
            win.Show();
        }
        /// <summary>
        /// 新增一条名次
        /// </summary>
        /// <param name="obj"></param>
        private void OnAddBouns(object obj)
        {
            if (Competition == null) return;
            Competition.Bounses.Add(new Bouns());
        }
        /// <summary>
        /// 新增一条级别
        /// </summary>
        /// <param name="obj"></param>
        private void OnAddLevel(object obj)
        {
            if (Competition == null) return;
            Competition.CompetitionLevels.Add(new CompetitionLevel());
        }

        private void OnSelectedTemplate(Competition competition)
        {
            if (competition == null) return;
            Competition.ID = 0;
            Competition.Name = competition.Name;
            Competition.Chips = competition.Chips;
            Competition.SinglePoints = competition.SinglePoints;
            Competition.DownsideBonus = competition.DownsideBonus;
            Competition.MaxMember = competition.MaxMember;
            Competition.ManCastRatio = competition.ManCastRatio;
            Competition.DownsidePoints = competition.DownsidePoints;

            Competition.CompetitionLevels.Clear();
            foreach (var level in competition.CompetitionLevels)
            {
                Competition.CompetitionLevels.Add(new CompetitionLevel()
                                                      {
                                                          AfterRestTime = level.AfterRestTime,
                                                          AppendGamble = level.AppendGamble,
                                                          Blind = level.Blind,
                                                          CompetitionTime = level.CompetitionTime,
                                                          Level = level.Level
                                                      });
            }

            Competition.Bounses.Clear();
            foreach (var bouns in competition.Bounses)
            {
                Competition.Bounses.Add(new Bouns() {Rank = bouns.Rank, Scale = bouns.Scale});
            }
            OnPropertyChanged("Competition");
        }

        public Competition Competition
        {
            get { return _Competition ?? (_Competition = new Competition()); }
            set
            {
                _Competition = value;

                OnPropertyChanged("Competition");
                OnPropertyChanged("Members");
                OnPropertyChanged("Tableses");
                OnPropertyChanged("Rankings");
            }
        }
        /// <summary>
        /// 更新还剩余的比赛人数
        /// </summary>
        public void UpdateRanking()
        {
            _rankings.Clear();
            if (Competition != null && Competition.ID > 0)
            {
                var members = Members;
                foreach (var memberSeat in members)
                {
                    //排除出局的人
                    if (memberSeat.OutState == 1) continue;
                    var ranking = new Ranking { CardNo = memberSeat.CardNo, Name = memberSeat.CardNo, Rank = memberSeat.Rank, CompetitionID = Competition.ID };
                    _rankings.Add(ranking);
                }
            }
        }

        public List<Competition> Competitions
        {
            get
            {
                _Competitions = new CompetitionService().SearchCompetition();
                return _Competitions;
            }
            set { _Competitions = value; }
        }

        /// <summary>
        /// 已经报名的人
        /// </summary>
        public List<MemberSeat> Members
        {
            get
            {
                if (Competition == null || Competition.ID <= 0) return null;
                _members = new MemberService().GetMemberCompetitionID(Competition.ID);
                return _members;
            }
            set { _members = value; }
        }

        public List<Tables> Tableses
        {
            get
            {
                _tableses = new TablesService().GetTables(Competition.ID);
                return _tableses;
            }
        }

        public List<Ranking> Rankings
        {
            get
            {
                return _rankings;
            }
            set { _rankings = value; }
        }

        public List<Competition> AllCompetitions
        {
            get
            {
                _AllCompetitions = CompetitionService.Instance().GetAllCompetition(CompetitionName, PageIndex, 20);
                OnPropertyChanged("AllCompetitionTotal");
                return _AllCompetitions;
            }
            set { _AllCompetitions = value; }
        }

        public int AllCompetitionTotal
        {
            get
            {
                _allCompetitionTotal = CompetitionService.Instance().GetAllCompetitionTotal(CompetitionName);
                return _allCompetitionTotal;
            }
        }

        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                _pageIndex = value;
                OnPropertyChanged("AllCompetitions");
            }
        }
    }
}
